package com.my.netty.nio.net;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.SocketChannel;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

public class BlockNioClient {

    public static void main(String[] args) throws IOException {
        //NIO TCP客户端

        //1.创建SocketChannel
        SocketChannel sc = SocketChannel.open();
        //2.连接服务器
        sc.connect(new InetSocketAddress("localhost", 8080));

        //3.向服务器发送数据
        Scanner scanner = new Scanner(System.in);
        String line;
        do {
            System.out.println("输入向服务器发送的数据：");
            line = scanner.nextLine();
            sc.write(StandardCharsets.UTF_8.encode(line));
        } while (!"q".equalsIgnoreCase(line));

        //4.关闭连接
        sc.close();
        scanner.close();
    }
}
